package cn.topcodes.tcsf.app.web.controller.admin;

import cn.topcodes.tcsf.app.service.impl.ConfigService;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Controller("appOtherController")
@RequestMapping(value = "/admin/other")
public class OtherController {

    public static final String FILE_PATH = "/admin/other/dl-image";

    @Autowired
    private ConfigService configService;

    @RequestMapping(value = "dl-image", method = RequestMethod.GET)
    public void downloadImg(@RequestParam(required = true) String path, HttpServletResponse response) throws IOException {
        String filepath = configService.getFileFolderPath() + path;
        File file = new File(filepath);
        if (!file.exists()) {
            response.sendError(HttpStatus.SC_NOT_FOUND);
            return;
        }
        FileInputStream fin = new FileInputStream(file);
        IOUtils.copy(fin, response.getOutputStream(), 1024);
    }
}

